// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 .
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : apb_monitor.sv
// Author        : 
// Created On    : 2022-08-25 15:47
// Last Modified : 
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

`ifndef __APB_MONITOR_SV__
`define __APB_MONITOR_SV__

class apb_monitor extends uvm_monitor;
	virtual apb_interface vif;
	uvm_analysis_port #(apb_transaction) ap;
	
	`uvm_component_utils(apb_monitor)
	
	extern function new(string name = "apb_monitor", uvm_component parent=null);
	extern virtual function void build_phase(uvm_phase phase);
	extern virtual task run_phase(uvm_phase phase);
	extern virtual task receive_pkt();
endclass: apb_monitor

function apb_monitor::new(string name = "apb_monitor", uvm_component parent=null);
	super.new(name, parent);
endfunction: new

function void apb_monitor::build_phase(uvm_phase phase);
	super.build_phase(phase);
	`uvm_info("apb_monitor", "build_phase is called", UVM_HIGH);
	if(!uvm_config_db #(virtual apb_interface)::get(this, "", "vif", vif))begin
		`uvm_fatal("apb_monitor", "virtual interface get fatal");
	end
	ap   = new("ap", this);
endfunction: build_phase

task apb_monitor::run_phase(uvm_phase phase);
    super.run_phase(phase);
	fork 
		this.receive_pkt();
	join_none
endtask: run_phase

task apb_monitor::receive_pkt();
	apb_transaction data = new();
	while(1) begin
		@this.vif.mon_cb;
        if(this.vif.mon_cb.psel == 1'b1 && this.vif.mon_cb.pready == 1'b1 && this.vif.mon_cb.penable == 1'b0)begin
            data.addr = this.vif.mon_cb.paddr;
            data.kind = this.vif.mon_cb.pwrite ? apb_transaction::WRITE : apb_transaction::READ;
        end
        else if(this.vif.mon_cb.psel == 1'b1 && this.vif.mon_cb.penable == 1'b1)begin
            if(data.kind == apb_transaction::READ) data.data = this.vif.mon_cb.prdata;
            else                                   data.data = this.vif.mon_cb.pwdata;
            ap.write(data);
            `uvm_warning("apb_monitor", "send a data");
        end
	end
endtask: receive_pkt

`endif
